package com.example.wanmengnovel;

import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.*;

@WebServlet("/login")
public class login extends HttpServlet {
    public Connection getConnection() throws SQLException, ClassNotFoundException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        System.out.println("JDBC驱动加载成功！");
        String url = "jdbc:mysql://localhost:3306/users";
        String user = "root";
        String password = "lcw0909.";
        Connection connection = DriverManager.getConnection(url, user, password);
        return connection;
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int mun = 0;
        try {
            mun = Integer.valueOf(req.getParameter("username"));
        } catch (NumberFormatException e) {
            resp.setContentType("text/html;charset=utf-8");
            resp.getWriter().print("<script language='javascript'>" +
                    "alert('账号输入有误，请输入数字！');" +
                    "window.location.href='index.jsp';</script>')");
        }
//如果用户输入账号不符合格式，则返回弹窗错误

        try {
            Connection connection = getConnection();
            String sql = "SELECT userAccount, userPassword FROM novelusers WHERE userAccount = ?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, mun);
// 执行查询并获取结果集
            ResultSet resultSet = preparedStatement.executeQuery();
// 处理结果集
            boolean flag = false;
            if (resultSet.next()) {
                int userAccount = resultSet.getInt("userAccount");
                String userPassword = resultSet.getString("userPassword");
                if (mun == userAccount && req.getParameter("password").equals(userPassword)) {
                    flag = true;
                }
            }
            if (flag) {
                Cookie cookie = new Cookie("username",req.getParameter("username"));
                cookie.setMaxAge(86400);
                resp.addCookie(cookie);
                RequestDispatcher rd = req.getRequestDispatcher("/WEB-INF/Interface.jsp");
                rd.forward(req, resp);
            }else {
                resp.setContentType("text/html;charset=utf-8");
                resp.getWriter().print("<script language='javascript'>" +
                        "alert('账号或密码错误，请重新输入！');" +
                        "window.location.href='index.jsp';</script>')");
            }
// 关闭 ResultSet 和 PreparedStatement
            resultSet.close();
            preparedStatement.close();
        } catch (SQLException e) {
            resp.setContentType("text/html;charset=utf-8");
            resp.getWriter().print("<script language='javascript'>" +
                    "alert('账号不存在，请先注册！');" +
                    "window.location.href='index.jsp';</script>')");
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}
